package JPetStore.web.servlet;

import JPetStore.domain.Account;
import JPetStore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UserNameExistServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("newname");
        AccountService accountService = new AccountService();
        boolean result;
        result=accountService.is_repeat(username);
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        // 根据布尔值返回不同的响应
        if (result) {
            resp.getWriter().write("{\"success\": true}");
        } else {
            resp.getWriter().write("{\"success\": false}");
        }
    }
}
